package com.hyst.web;

import java.text.ParseException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.domain.PurchaseCourse;
import com.hyst.domain.PurchaseCourseView;
import com.hyst.domain.Student;
import com.hyst.service.PurchaseCourseService;

/**
 * @Description:购买课程记录表controller
 * @author：dy @Date：2018年09月11日
 */
@RestController
@RequestMapping("purchaseCourses")
@CrossOrigin(maxAge = 3600, origins = "*")
public class PurchaseCourseController {
	@Autowired
	private PurchaseCourseService purchaseCourseService;

	@GetMapping
	public ResultPage<PurchaseCourse> list(final PurchaseCourse purchaseCourse) {
		return ResultPage.success(purchaseCourseService.list(purchaseCourse));
	}

	@GetMapping("view")
	public ResultBean<PurchaseCourse> view(final String uniqueid) {
		return ResultBean.success(purchaseCourseService.getView(uniqueid));
	}

	@GetMapping("students")
	public ResultPage<Student> getStudents() {
		return ResultPage.success(purchaseCourseService.getStudents());
	}

	@PostMapping
	public ResultBean<PurchaseCourse> creat(final PurchaseCourse purchaseCourse) {
		System.out.println(purchaseCourse);
		return ResultBean.success(purchaseCourseService.insert(purchaseCourse));
	}

	/**
	 * @Description: 根据主键查询
	 * @param uniqueid
	 * @return ResultBean<PurchaseCourse>
	 */
	@GetMapping("{uniqueid}")
	public ResultBean<PurchaseCourse> getByPriKey(@PathVariable("uniqueid") final String uniqueid) {
		return ResultBean.success(purchaseCourseService.getByPriKey(uniqueid));
	}

	/**
	 * @Description: 根据主键更新全字段
	 * @param purchaseCourse
	 * @return ResultBean<PurchaseCourse>
	 */
	@PatchMapping("{uniqueid}")
	public ResultBean<PurchaseCourse> update(final PurchaseCourse purchaseCourse) {
		return ResultBean.success(purchaseCourseService.update(purchaseCourse));
	}

	/**
	 * @Description: 根据主键更新不为默认值的字段
	 * @param purchaseCourse
	 * @return ResultBean<PurchaseCourse>
	 */
	// @PutMapping("{uniqueid}")
	@PostMapping("{uniqueid}")
	public ResultBean<PurchaseCourse> updatePatch(@PathVariable("uniqueid") final String uniqueid,
			final PurchaseCourse purchaseCourse) {
		return ResultBean.success(purchaseCourseService.updateSelective(purchaseCourse));
	}

	/**
	 * @Description: 根据主键删除
	 * @param uniqueid
	 * @return ResultBean<PurchaseCourse>
	 */
	@DeleteMapping("{uniqueid}")
	public ResultBean<PurchaseCourse> delete(@PathVariable("uniqueid") final String uniqueid) {
		return ResultBean.success(purchaseCourseService.delete(uniqueid));
	}

	@PostMapping("addTimes")
	public ResultBean<PurchaseCourse> addTimes(final PurchaseCourse purchaseCourse)
			throws ParseException, CloneNotSupportedException {
		System.err.println(purchaseCourse);

		purchaseCourse.setHoursRemaining(purchaseCourse.getHoursEnrolled());
		return ResultBean.success(purchaseCourseService.addTimes(purchaseCourse));
	}
	@GetMapping("getByStudent")
	public ResultPage<PurchaseCourseView> getByStudent(final PurchaseCourseView view) {
		return ResultPage.success(purchaseCourseService.getByStudent(view));
	}
	/**还原课程*/
	@PostMapping("reduction")
	public ResultBean<Integer> reduction(String purchaseCourseId){
		return ResultBean.success(purchaseCourseService.reduction(purchaseCourseId));
	}

	@PostMapping("updateClass")
	public ResultBean updateClassTime(final PurchaseCourse PurchaseCourse) throws Exception{
		purchaseCourseService.updateClassTime(PurchaseCourse);
		return ResultBean.success(null);
	}
}
